﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Pipes;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        /// <summary>
        /// 匿名管道客户端程序
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            if (args.Length > 0)
            {
                using (PipeStream pipeClient = new AnonymousPipeClientStream(PipeDirection.In, args[0]))
                {
                    // Show that anonymous Pipes do not support Message mode.
                    try
                    {
                        Console.WriteLine("【Client】Setting ReadMode to \"Message\".");
                        pipeClient.ReadMode = PipeTransmissionMode.Message;
                    }
                    catch (NotSupportedException e)
                    {
                        Console.WriteLine("[CLIENT] Execption:\n    {0}", e.Message);
                    }

                    Console.WriteLine("[CLIENT] Current TransmissionMode: {0}.", pipeClient.TransmissionMode);
                    
                    using (StreamReader sr = new StreamReader(pipeClient))
                    {
                        string temp;
                        do
                        {
                            Console.WriteLine("[CLIENT] Wait for sync...");
                            temp = sr.ReadLine();
                            Console.WriteLine("[CLIENT] Echo: " + temp);
                        }
                        while (temp != "N");
                    }
                }
            }
            Console.Write("[CLIENT] Get Command to quit.");
            Console.ReadLine();
        }

    }

}
